package com.tsugun.mediaplayer.presenter.utils

import android.app.Activity
import android.content.Context
import android.content.res.ColorStateList
import android.os.Build
import android.preference.PreferenceManager
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.TextView

import com.tsugun.mediaplayer.R
import com.tsugun.mediaplayer.view.activity.BaseActivity

import java.io.File
import java.util.ArrayList

/**
 * 设置保存参数工具类
 * Created by shize on 2017/8/1.
 */

object SettingPreferenceHelper {
    var BACKGROUND_COLOR: Int = 0 // 模式背景色
    var TEXT_COLOR: Int = 0 // 模式文字颜色
    // 标题栏默认颜色
    private val TITLE_DEFAULT_COLOR = 0xffef5350.toInt() // 默认
    private val TITLE_RED_COLOR = 0xffff3c3c.toInt() // 姨妈红
    private val TITLE_PURPLE_COLOR = 0xffA757A8.toInt() // 基佬紫
    private val TITLE_ORANGE_COLOR = 0xffFF8C00.toInt() // 伊藤橙
    private val TITLE_BLACK_COLOR = 0xff333333.toInt() // 高端黑
    private val TITLE_GREY_COLOR = 0xff888888.toInt() // 低调灰

    private val TEXT_COLOR_LIGHT = 0xffcccccc.toInt() // 夜间模式文字颜色
    private val TEXT_COLOR_DARK = 0xff444444.toInt() // 非夜间模式文字颜色
    private val BACK_COLOR_DARK = 0xff111111.toInt() // 夜间模式背景色
    private val BACK_COLOR_LIGHT = 0xfffafafa.toInt() // 非夜间模式背景色
    // 颜色标识
    private val DEFAULT = "默认"
    private val RED = "姨妈红"
    private val PURPLE = "基佬紫"
    private val ORANGE = "伊藤橙"
    private val BLACK = "高端黑"
    private val GREY = "低调灰"

    /**
     * 获取主题颜色设置参数

     * @return 主题颜色
     */
    fun getThemeColorPreference(context: Context): Int {
        var color = 0
        val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
        if (sharedPreferences != null && !sharedPreferences.getBoolean(context
                .getString(R.string.key_theme_night), false)) {
            when (sharedPreferences.getString(context.getString(R.string.key_theme_color),
                    context.getString(R.string.str_setting_list_default))) {
                DEFAULT -> color = TITLE_DEFAULT_COLOR
                RED -> color = TITLE_RED_COLOR
                PURPLE -> color = TITLE_PURPLE_COLOR
                ORANGE -> color = TITLE_ORANGE_COLOR
                BLACK -> color = TITLE_BLACK_COLOR
                GREY -> color = TITLE_GREY_COLOR
            }
        } else {
            color = TITLE_BLACK_COLOR
        }
        return color
    }

    /**
     * 设置夜间模式
     */
    fun setNightModeTheme(activity: BaseActivity) {
        if (isNight(activity)) {
            setNightMode(getChildViews(activity))
            BACKGROUND_COLOR = BACK_COLOR_DARK
            TEXT_COLOR = TEXT_COLOR_LIGHT
        } else {
            setNormalMode(getChildViews(activity))
            BACKGROUND_COLOR = BACK_COLOR_LIGHT
            TEXT_COLOR = TEXT_COLOR_DARK
        }
    }

    /**
     * 是否为夜间模式
     */
    private fun isNight(activity: Activity): Boolean {
        val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(activity)
        return sharedPreferences.getBoolean(activity.getString(R.string.key_theme_night), false)
    }

    /**
     * 设置非夜间模式需要更改的控件属性

     * @param views 所有子控件
     */
    private fun setNormalMode(views: List<View>) {
        val colorStateList = ColorStateList(arrayOf(IntArray(0)),
                intArrayOf(TEXT_COLOR_DARK))
        for (view in views) {
            if (view is TextView) {
                view.setTextColor(TEXT_COLOR_DARK)
                setTextDrawableColor(colorStateList, view)
            }
            val o = view.tag
            if (isNightBg(view, o)) {
                view.setBackgroundColor(BACK_COLOR_LIGHT)
            }
            if (isNightIcon(view, o)) {
                (view as ImageButton).imageTintList = colorStateList
            }
        }
    }

    private fun setTextDrawableColor(colorStateList: ColorStateList, view: TextView) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            view.compoundDrawableTintList = colorStateList
        }
    }

    /**
     * 设置夜间模式需要更改的控件属性

     * @param views 所有子控件
     */
    private fun setNightMode(views: List<View>) {
        val colorStateList = ColorStateList(arrayOf(IntArray(0)),
                intArrayOf(TEXT_COLOR_LIGHT))
        for (view in views) {
            if (view is TextView) {
                view.setTextColor(TEXT_COLOR_LIGHT)
                setTextDrawableColor(colorStateList, view)
            }
            val o = view.tag
            if (isNightBg(view, o)) {
                view.setBackgroundColor(BACK_COLOR_DARK)
            }
            if (isNightIcon(view, o)) {
                (view as ImageButton).imageTintList = colorStateList
            }
        }
    }

    /**
     * 获取子view
     */
    private fun getChildViews(activity: BaseActivity): List<View> {
        val view = activity.window.decorView
        return getAllChildViews(view)
    }

    /**
     * 获取所有子view中的控件(利用递归获取所有控件)

     * @param view 父类view
     * *
     * @return view 的 list集合
     */
    private fun getAllChildViews(view: View): List<View> {
        val childViews = ArrayList<View>()
        if (view is ViewGroup) {
            val viewGroup = view
            var o = viewGroup.tag
            if (isNightTag(viewGroup, o)) {
                for (i in 0..viewGroup.childCount - 1) {
                    val v = viewGroup.getChildAt(i)
                    o = v.tag
                    if (isNightTag(viewGroup, o)) {
                        childViews.add(v)
                    }
                    childViews.addAll(getAllChildViews(v))
                }
            }
        }
        return childViews
    }

    /**
     * 是否为夜间模式图标
     */
    private fun isNightIcon(view: View, o: Any?): Boolean {
        return o != null && o == view.context.getString(R.string.key_theme_night_icon) &&
                view is ImageButton
    }

    /**
     * 是否为夜间模式背景
     */
    private fun isNightBg(view: View, o: Any?): Boolean {
        return o != null && o == view.context.getString(R.string.key_theme_night)
    }

    /**
     * 是否是不需要夜间更改的控件
     */
    private fun isNightTag(viewGroup: ViewGroup, o: Any?): Boolean {
        return o == null || o != viewGroup.context.getString(R.string.key_theme_no_night)
    }

    /**
     * fragment设置夜间模式
     */
    fun setNightModeTheme(view: View, baseActivity: BaseActivity) {
        val viewList = getAllChildViews(view)
        if (isNight(baseActivity)) {
            setNightMode(viewList)
        } else {
            setNormalMode(viewList)
        }
    }

    /**
     * 获取锁屏设置信息

     * @return 是否开启锁屏
     */
    fun getLockScreenSetting(context: Context): Boolean {
        val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
        return sharedPreferences.getBoolean(context.getString(R.string.pref_key_other_lock), true)
    }

    /**
     * 删除缓存
     */
    fun deleteCache(context: Context) {
        val file = File(BaseActivity.CACHE_DIR)
        val files = file.listFiles()
        for (file1 in files) {
            Log.e(LogHelper.TAG, "onPreferenceClick: fileCount=" + file.totalSpace)
            SystemHelper.deleteImageFile(context, file1)
        }
    }
}
